'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Windows.Forms
Imports System.Net

Public Class iiNet
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)

        Try
            'set rollover date
            SetRolloverDate(ConfigurationData, ISPReturnData)

            Dim strHTMLData As String

            'usage
            strHTMLData = ISPGet("https://toolbox.iinet.net.au/cgi-bin/new/volume_usage_xml.cgi?username=" & ConfigurationData.Username & "&action=login&password=" & ConfigurationData.Password, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            'https://toolbox.iinet.net.au/cgi-bin/new/volume_usage_xml.cgi?username=USERNAME&action=login&password=PASSWORD

            If strHTMLData.Contains("XML Parsing Error") = True Then
                ISPReturnData.ErrorMessage = "Login Failed"
                Exit Sub
            End If

            If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

            '##########################################################

            'process response
            'check for non-free new plan
            Dim OldPage As Boolean = True
            Dim FreeTotal As Single

            If strHTMLData.ToLower.Contains("non-free") = True Then
                'new
                OldPage = False
                ISPReturnData.PeakDownload = CSng(Val(ExtractText(strHTMLData, "<B>Total</B>", 40, "<B>", "</B>")))
                FreeTotal = CSng(Val(ExtractText(strHTMLData, "<B>Total</B>", 55, "<B>", "</B>")))
            Else
                ISPReturnData.PeakDownload = CSng(Val(ExtractText(strHTMLData, "<B>Total</B>", 40, "<B>", "</B>")))
                ISPReturnData.OffpeakDownload = CSng(Val(ExtractText(strHTMLData, "<B>Total</B>", 55, "<B>", "</B>")))
                FreeTotal = CSng(Val(ExtractText(strHTMLData, "<B>Total</B>", 110, "<B>", "</B>")))
            End If

            ISPReturnData.LastUpdated = Now

            '##########################################################

            ProcessISPData(ConfigurationData, ISPReturnData)

            'check for error
            If ISPReturnData.ErrorMessage <> "" Then Exit Sub

            '##########################################################

            'create graph data

            If OldPage = True Then
                'peak
                ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), ISPReturnData.PeakNow))

                'offpeak
                ISPReturnData.GraphItemList.Add(New GraphItem("Offpeak " & Format(System.Math.Floor((ISPReturnData.OffpeakDownload / Val(ConfigurationData.OffpeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.OffpeakDownload & " MB)", ISPReturnData.OffpeakDownload, CSng(Val(ConfigurationData.OffpeakUsageLimit)), ISPReturnData.OffpeakNow))
            Else
                'nonfree
                ISPReturnData.GraphItemList.Add(New GraphItem("Usage " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), ISPReturnData.PeakNow))
            End If

            'days elapsed
            ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

            '##########################################################

            'create stats data
            If OldPage = True Then
                ISPReturnData.TextItemList.Add(New TextItem("Peak Left :", ISPReturnData.PeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Peak Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Offpeak D/L Left :", ISPReturnData.OffpeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Offpeak Avg. Left :", ISPReturnData.OffpeakUsageRemainingPerDayDescription))
            Else
                ISPReturnData.TextItemList.Add(New TextItem("Usage Left :", ISPReturnData.PeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Usage Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))
            End If

            ISPReturnData.TextItemList.Add(New TextItem("Freezone :", FreeTotal & " MB"))
            If ISPReturnData.ShapedNow = False Then
                ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "NO"))
            Else
                ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "YES"))
            End If

            '##########################################################

            'process alerts
            ProcessAlerts(ConfigurationData, ISPReturnData)

        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try

    End Sub

End Class
